package com.mall.user.services;


import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.IUserLoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Service
public class IUserLoginServiceImpl implements IUserLoginService {

    //解析token
    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        String token = checkAuthRequest.getToken();
        String str = null;
        try {
        str =JwtTokenUtils.builder().token(token).build().freeJwt();
        }catch (ValidateException e){
            //解析失败
            CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
            checkAuthResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED .getCode());
            checkAuthResponse.setMsg(SysRetCodeConstants.TOKEN_VALID_FAILED .getMessage());
            return checkAuthResponse;
        }
        //成功
        CheckAuthResponse check = new CheckAuthResponse();
        check.setCode(SysRetCodeConstants.SUCCESS.getCode());
        check.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        check.setUserinfo(str);
        return check;
    }
}
